/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package locusts.lib.geom;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author hamish
 */
public class SpaceShip implements Shape, Cloneable {

    private final GeneralPath path;

    public SpaceShip(double x, double y, double w, double h, boolean moving) {
        path = createSpaceship(x, y, w, h, moving);
    }

    public SpaceShip(double x, double y, double w, double h) {
        this(x, y, w, h, false);
    }
    public SpaceShip(double x, double y, double size) {
        this(x, y, size, size);
    }

    protected SpaceShip(SpaceShip that) {
        this.path = (GeneralPath) that.path.clone();
    }

    private static GeneralPath createSpaceship(double x, double y, double w,
            double h, boolean moving) {
        final double opp = Math.sqrt(h * h - w * w / 4);
        final GeneralPath p = new GeneralPath();
        {
            final double x0 = (x - w / 2d);
            final double x1 = 0;
            final double x2 = (x + w / 2d);

            final double y0 = (y - opp / 2d);
            final double y1 = (y - opp / 3d);
            final double y2 = (y + opp / 2d);

            p.moveTo((float) x1, (float) y2);
            p.lineTo((float) x2, (float) y0);
            //p.lineTo(x0, y0);
            p.curveTo((float) (x2 + x1) / 2, (float) y1, (float) (x1 + x0) / 2,
                    (float) y1, (float) x0, (float) y0);
            p.closePath();
        }
        if (moving) {
            final double x0 = (x - w / 4d);
            final double x1 = 0;
            final double x2 = (x + w / 4d);
            final double ya = (y - opp);
            final double y0 = (y - opp * 0.6d);
            final double y1 = (y - opp * 0.4d);
            p.moveTo((float) x0, (float) y0);
            p.lineTo((float) x1, (float) ya);
            p.lineTo((float) x2, (float) y0);
            p.curveTo((float) (x2 + x1) / 2, (float) y1, (float) (x1 + x0) / 2,
                    (float) y1, (float) x0, (float) y0);
            p.closePath();

        }
        return p;
    }

    public Rectangle getBounds() {
        return path.getBounds();
    }

    public Rectangle2D getBounds2D() {
        return path.getBounds2D();
    }

    public boolean contains(double x, double y) {
        return path.contains(x, y);
    }

    public boolean contains(Point2D p) {
        return path.contains(p);
    }

    public boolean intersects(double x, double y, double w, double h) {
        return path.intersects(x, y, w, h);
    }

    public boolean intersects(Rectangle2D r) {
        return path.intersects(r);
    }

    public boolean contains(double x, double y, double w, double h) {
        return path.contains(x, y, w, h);
    }

    public boolean contains(Rectangle2D r) {
        return path.contains(r);
    }

    public PathIterator getPathIterator(AffineTransform at) {
        return path.getPathIterator(at);
    }

    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        return path.getPathIterator(at, flatness);
    }

    @Override
    public SpaceShip clone() {
        return new SpaceShip(this);
    }
}
